/*
 * 文件名:LocationServiceImpl.java
 * 创建时间:2010-10-01
 * 版本:2.0
 * 版权所有:杨涛 walle1027@gmail.com
 */
package com.yangtao.wms.master.location.service.impl;

import com.yangtao.framework.hibernate.Selector;
import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.wms.dao.LocationDao;
import com.yangtao.wms.dao.LocationGroupDao;
import com.yangtao.wms.domain.Location;
import com.yangtao.wms.domain.LocationGroup;
import com.yangtao.wms.master.location.service.LocationService;
import com.yangtao.wms.master.location.web.LocationModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 代码生成器 v-2.0
 * @version 2.0
 */
@Service()
public class LocationServiceImpl extends BaseService implements LocationService {

	@Autowired
	protected LocationDao locationDao;
	@Autowired
	protected LocationGroupDao locationGroupDao;

	/**
	 * 查询记录，并且分页
	 *
	 * @param model 页面参数
	 */
	@Override
	public LocationModel list(LocationModel model) {
		Selector selector = new Selector();
		selector.add(".locationGroup");
		locationDao.findPageByCondition(selector, model.getPage(), model.getCondition());
		return model;
	}

	/**
	 * 取得唯一记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public LocationModel loadLocation(LocationModel model) {
		if (StringHelper.isNotEmpty(model.getLocation().getId())) {
			Selector selector = new Selector();
			selector.add(".locationGroup");
			model.setLocation(locationDao.loadForReview(model.getLocation().getId(), selector));
		}
		return model;
	}

	/**
	 * 新增记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public LocationModel addLocation(LocationModel model) {
		//VO
		Location locationVO = model.getLocation();
		//PO
		Location locationPO = new Location();
		mapper.map(locationVO, locationPO);
		//构造关联从对象
		//从model中取出关联子对象列表
		List<LocationGroup> locationGroupVO = locationVO.getLocationGroup();
		if (locationGroupVO != null && locationGroupVO.size() > 0) {
			List<LocationGroup> locationGroupPO = new ArrayList<LocationGroup>();
			for (LocationGroup voItem : locationGroupVO) {
				if (voItem != null) {
					LocationGroup poItem = new LocationGroup();
					mapper.map(voItem, poItem);
					//设置额外属性
					setLocationGroupProperties(poItem, model);
					poItem.setLocation(locationPO);
					locationGroupPO.add(poItem);
				}
			}
			locationPO.setLocationGroup(locationGroupPO);
		}
		//保存对象
		locationDao.save(locationPO);
		//增加保存成功的消息
		model.addInfo("add.success");
		return model;
	}

	/**
	 * 更新记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public LocationModel updateLocation(LocationModel model) {
		//VO
		Location locationVO = model.getLocation();
		//主键
		String locationId = locationVO.getId();
		//PO
		Location locationPO = locationDao.load(locationId);
		//复制页面属性
		mapper.map(locationVO, locationPO);
		//TODO 增加其他属性
		//TODO 更新关联对象
		//构造关联从对象
		//从model中取出关联子对象列表
		//先遍历PO
		List<LocationGroup> locationGroupPO = locationPO.getLocationGroup();
		List<LocationGroup> locationGroupVO = locationVO.getLocationGroup();
		if (locationGroupPO == null) {
			locationGroupPO = new ArrayList<LocationGroup>();
			locationPO.setLocationGroup(locationGroupPO);
		}
		if (locationGroupVO == null) {
			locationGroupVO = new ArrayList<LocationGroup>();
		}
		List<LocationGroup> _locationGroupVO = new LinkedList<LocationGroup>();
		_locationGroupVO.addAll(locationGroupVO);
		//更新或者删除
		for (Iterator<LocationGroup> i = locationGroupPO.iterator(); i.hasNext(); ) {
			LocationGroup poItem = i.next();
			boolean isDeletedItem = true;
			for (Iterator<LocationGroup> j = _locationGroupVO.iterator(); j.hasNext(); ) {
				LocationGroup voItem = j.next();
				if (poItem.getId().equals(voItem.getId())) {
					//更新明细
					mapper.map(voItem, poItem);
					isDeletedItem = false;
					j.remove();
				}
			}
			if (isDeletedItem) {
				i.remove();
				locationGroupDao.delete(poItem);
			}
		}
		//新增
		if (_locationGroupVO.size() > 0) {
			for (LocationGroup voItem : _locationGroupVO) {
				LocationGroup poItem = new LocationGroup();
				mapper.map(voItem, poItem);
				//设置额外属性
				setLocationGroupProperties(poItem, model);
				//TODO 根据级联设置，来决定操作
				poItem.setLocation(locationPO);
				locationGroupPO.add(poItem);
			}
		}
		//增加保存成功的消息
		model.addInfo("update.success");
		return model;
	}

	/**
	 * 删除记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public LocationModel deleteLocation(LocationModel model) {
		locationDao.delete(model.getLocation().getId());
		model.addInfo("delete.success");
		return model;
	}

	/**
	 * 设置关联对象:库位组 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param locationGroup 关联对象
	 * @param model         页面参数类
	 */
	protected void setLocationGroupProperties(final LocationGroup locationGroup, final LocationModel model) {
	}
}
